package bigo.todolist.avocado;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

public class InteractiveArrayAdapter extends ArrayAdapter<Model> {

	private final List<Model> list; //Model to set up part of the list adapter with
	private final Activity context;
	private final String username;
	private final ArrayList<String> checks; //The list of checked tasks

	public InteractiveArrayAdapter(Activity context, List<Model> list, String username, ArrayList<String> checks) {
		super(context, R.layout.rowbuttonlayout, list);
		this.context = context;
		this.list = list;
		this.username = username;
		this.checks = checks;
	}

	static class ViewHolder {
		protected TextView text;
		protected CheckBox checkbox;
	}

	/**
	 * Create a view with the list adapter. It contains the task name and a checkable box
	 */
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = null;
		if (convertView == null) {
			LayoutInflater inflator = context.getLayoutInflater();
			view = inflator.inflate(R.layout.rowbuttonlayout, null);
			final ViewHolder viewHolder = new ViewHolder();
			viewHolder.text = (TextView) view.findViewById(R.id.label); //Add label for name
			viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check); //Add check box
			viewHolder.checkbox
					.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { //Check box listener

						public void onCheckedChanged(CompoundButton buttonView,
								boolean isChecked) {
							Model element = (Model) viewHolder.checkbox
									.getTag(); //get the checkbox
							element.setSelected(buttonView.isChecked()); //Set if it's selected or not
							boolean check = element.isSelected(); //Get its selection
							int count = 0;
							for(String s:checks) //Check how many instances of this check we are keeping track of
							{
								if(s.equals(element.getName()))
									count++;
							}
							if((check == true) && (checks.contains(element.getName()) == false)) //If we do not know that this box is checked yet and is not in the list, add it to the checked list
							{
								checks.add(element.getName());
							}
							else if(check == false || count>1) //If there are too many instances in the checked list or the check is now false, remove the task from the checked list
							{
								checks.remove(element.getName());
							}

						}
					});
			view.setTag(viewHolder);
			viewHolder.checkbox.setTag(list.get(position));
		} else {
			view = convertView;
			((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
		}
		ViewHolder holder = (ViewHolder) view.getTag();
		holder.text.setText(list.get(position).getName());
		holder.checkbox.setChecked(list.get(position).isSelected());
		return view;
	}
}
